package com.tjbklx1.redis.chap01;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Redis in Action ,chap1.2案例,五中数据类型的简单使用
 * @author tjbklx1
 * @date 2018-12-1
 */
public class App01 {

	/**
	 * String 方法 : set,get,del
	 */
	@Test
	public void getSetString() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String value = redis.get("hello"); //返回值是String
			System.out.println(value);

			redis.set("hello", "world");

			value = redis.get("hello");
			System.out.println(value);

			redis.del("hello");

			value = redis.get("hello");
			System.out.println(value);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}

	}

	/**
	 * list : del,lpush,rpush,lpop,rpop,lrange,lindex
	 */
	@Test
	public void list() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "list-key";
			
			redis.del(key);
			
			printList(redis, key);
			redis.rpush(key, "item");
			redis.rpush(key, "item2");
			redis.rpush(key, "item");
			printList(redis, key);

			System.out.println(redis.lindex(key, 0));  //查询第一个元素
			redis.lpop(key); //删除左边第一个元素
			printList(redis, key);
		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}

	}

	private void printList(Jedis redis, String key) {
		List<String> resultList = redis.lrange(key, 0, -1); //返回值是List
		if (resultList != null && resultList.size() > 0) {
			System.out.println("############################");
			for (int i = 0; i < resultList.size(); i++) {
				System.out.println(i + " : " + resultList.get(i));
			}
			System.out.println("############################");
		}
	}

	/**
	 * set : del, sadd,sismember,srem,smembers
	 */
	@Test
	public void set() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "set-key";
			
			redis.del(key); //删除KEY 可以重复验证
			
			redis.sadd(key, "item1");
			redis.sadd(key, "item2");
			redis.sadd(key, "item3");
			redis.sadd(key, "item1");
			printSet(redis, key);  // 不包含重复元素
			System.out.println("item4 是否存在 :"+redis.sismember(key, "item4")); //检查是否是包含元素
			System.out.println("item1 是否存在 :"+redis.sismember(key, "item1"));

			System.out.println("删除 item1 :"+redis.srem(key, "item1")); //删除元素
			System.out.println("删除 item2 :"+redis.srem(key, "item2"));
			printSet(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printSet(Jedis redis, String key) {
		Set<String> resultSet = redis.smembers(key);  // 返回值是SET
		System.out.println("############################");
		for (String str : resultSet) {
			System.out.println(str);
		}
		System.out.println("############################");
	}

	/**
	 * del,hset,hget,hdel,hgetall
	 */
	@Test
	public void hash() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "hash-key";
			
			redis.del(key);
			
			redis.hset(key, "No1", "item1");
			redis.hset(key, "No2", "item2");
			redis.hset(key, "No3", "item3");
			redis.hset(key, "No4", "item4");
			printHash(redis, key); // 发现是无顺序的
			System.out.println("删除 No4 :"+redis.hdel(key, "No4"));
			System.out.println("获取 No1 :"+redis.hget(key, "No1"));
			printHash(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printHash(Jedis redis, String key) {
		Map<String, String> map = redis.hgetAll(key);  //返回的是Map
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
		}
	}

	/**
	 * zset: del,zadd,zrem,zrangebyscore,zrange
	 */
	@Test
	public void zset() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "zset-key";
			
			redis.del(key);
			
			redis.zadd(key, 100, "item1");
			redis.zadd(key, 80, "item2");
			redis.zadd(key, 200, "item3");
			redis.zadd(key, 1000, "item4");
			printZSet(redis, key); //根据score排序,从小到大
			System.out.println("删除ITEM  :"+redis.zrem(key, "item")); //删除不存在的元素
			System.out.println("删除item4 :"+redis.zrem(key, "item4"));
			printZSet(redis, key);
			printZSet2(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printZSet(Jedis redis, String key) {
		Set<String> set = redis.zrangeByScore(key, 0, 1000); //根据有序集合的score //返回值SET
		System.out.println("###########zrangeByScore#################");
		for (String str : set) {
			System.out.println(str);
		}
		System.out.println("############################");
	}
	
	private void printZSet2(Jedis redis, String key) {
		Set<String> set = redis.zrange(key, 0, -1); //根据有序集合的位置
		System.out.println("###########zrange#################");
		for (String str : set) {
			System.out.println(str);
		}
		System.out.println("############################");
	}
}
